﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace Chenxin.Lib.Hr.DAL
{
    /// <summary>
    /// 数据访问类Base_Permit_Define。
    /// </summary>
    public class Base_Permit_Define
    {
        public Base_Permit_Define()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public static bool Exists(Guid PermitID)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Base_Permit_Define where PermitID=@PermitID ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "PermitID", DbType.Guid, PermitID);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// 是否存在权限编号
        /// </summary>
        public static bool ExistsCode(string PermitCode)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Base_Permit_Define where PermitCode=@PermitCode ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "PermitCode", DbType.AnsiString, PermitCode);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.Hr.Entity.Base_Permit_Define model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Base_Permit_Define(");
            strSql.Append("PermitID,PermitCode,PermitName,PermitGroup,ShowLevel)");

            strSql.Append(" values (");
            strSql.Append("@PermitID,@PermitCode,@PermitName,@PermitGroup,@ShowLevel)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "PermitID", DbType.Guid, model.PermitID);
            db.AddInParameter(dbCommand, "PermitCode", DbType.AnsiString, model.PermitCode);
            db.AddInParameter(dbCommand, "PermitName", DbType.AnsiString, model.PermitName);
            db.AddInParameter(dbCommand, "PermitGroup", DbType.AnsiString, model.PermitGroup);
            db.AddInParameter(dbCommand, "ShowLevel", DbType.Int32, model.ShowLevel);
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.Hr.Entity.Base_Permit_Define model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Base_Permit_Define set ");
            strSql.Append("PermitCode=@PermitCode,");
            strSql.Append("PermitName=@PermitName,");
            strSql.Append("PermitGroup=@PermitGroup,");
            strSql.Append("ShowLevel=@ShowLevel");
            strSql.Append(" where PermitID=@PermitID ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "PermitID", DbType.Guid, model.PermitID);
            db.AddInParameter(dbCommand, "PermitCode", DbType.AnsiString, model.PermitCode);
            db.AddInParameter(dbCommand, "PermitName", DbType.AnsiString, model.PermitName);
            db.AddInParameter(dbCommand, "PermitGroup", DbType.AnsiString, model.PermitGroup);
            db.AddInParameter(dbCommand, "ShowLevel", DbType.Int32, model.ShowLevel);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid PermitID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Base_Permit_Define ");
            strSql.Append(" where PermitID=@PermitID ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "PermitID", DbType.Guid, PermitID);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.Hr.Entity.Base_Permit_Define GetModel(Guid PermitID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select PermitID,PermitCode,PermitName,PermitGroup,ShowLevel from Base_Permit_Define ");
            strSql.Append(" where PermitID=@PermitID ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "PermitID", DbType.Guid, PermitID);
            Chenxin.Lib.Hr.Entity.Base_Permit_Define model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select PermitID,PermitCode,PermitName,PermitGroup,ShowLevel ");
            strSql.Append(" FROM Base_Permit_Define ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            //按类型排序
            strSql.Append("order by PermitGroup,PermitName");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }


        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.Hr.Entity.Base_Permit_Define> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select PermitID,PermitCode,PermitName,PermitGroup,ShowLevel ");
            strSql.Append(" FROM Base_Permit_Define ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            //按类型排序
            strSql.Append("order by PermitGroup");
            List<Chenxin.Lib.Hr.Entity.Base_Permit_Define> list = new List<Chenxin.Lib.Hr.Entity.Base_Permit_Define>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.Hr.Entity.Base_Permit_Define ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.Hr.Entity.Base_Permit_Define model = new Chenxin.Lib.Hr.Entity.Base_Permit_Define();
            object ojb;
            ojb = dataReader["PermitID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.PermitID = new Guid(ojb.ToString());
            }
            model.PermitCode = dataReader["PermitCode"].ToString();
            model.PermitName = dataReader["PermitName"].ToString();
            model.PermitGroup = dataReader["PermitGroup"].ToString();
            ojb = dataReader["ShowLevel"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ShowLevel = (int)ojb;
            }
            return model;
        }

        #endregion  成员方法
    }
}

